package com.zxy.study.ui.widget;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Build;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.zxy.study.R;


/*
 *  @项目名：  study
 *  @包名：    com.zxy.study.ui.widget
 *  @文件名:   CameraRotateView
 *  @创建者:   zhangxy
 *  @创建时间:  2020/1/8 14:36
 *  @描述：    TODO
 */
public class CameraRotateView extends View {

	private Bitmap mBitmap;
	private Camera mCamera;
	private int degree;
	private Paint mPaint;
	private Point point = new Point(200, 50);

	private ObjectAnimator mAnimator;

	public CameraRotateView(Context context) {
		this(context, null);
	}

	public CameraRotateView(Context context, @Nullable AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public CameraRotateView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}

	@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
	public CameraRotateView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
		init();
	}

	private void init() {
		mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
		Bitmap newBitmap = Bitmap.createScaledBitmap(mBitmap, mBitmap.getWidth() * 3, mBitmap.getHeight() * 3, false);
		mBitmap.recycle();
		mBitmap = newBitmap;

		mPaint = new Paint();
		mCamera = new Camera();
		DisplayMetrics metrics = getResources().getDisplayMetrics();
		float newZ = -metrics.density * 6;
		mCamera.setLocation(0, 0, newZ);

		mAnimator = ObjectAnimator.ofInt(this, "degree", 0, 180);
		mAnimator.setDuration(2500);
		mAnimator.setInterpolator(new LinearInterpolator());
		mAnimator.setRepeatCount(ValueAnimator.INFINITE);
		mAnimator.setRepeatMode(ValueAnimator.REVERSE);
	}

	@SuppressWarnings("unused")
	public void setDegree(int degree) {
		this.degree = degree;
		invalidate();
	}

	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		mAnimator.start();
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		mAnimator.end();
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int wMode = MeasureSpec.getMode(widthMeasureSpec);
		if (wMode == MeasureSpec.EXACTLY) {
			int width = MeasureSpec.getSize(widthMeasureSpec);
			point.x = (width - mBitmap.getWidth()) / 2;
		} else {
			widthMeasureSpec = MeasureSpec.makeMeasureSpec(mBitmap.getWidth(), MeasureSpec.EXACTLY);
			point.x = 0;
		}

		int hMode = MeasureSpec.getMode(heightMeasureSpec);
		if (hMode == MeasureSpec.EXACTLY) {
			int height = MeasureSpec.getSize(heightMeasureSpec);
			point.y = (height - mBitmap.getHeight()) / 2;
		} else {
			heightMeasureSpec = MeasureSpec.makeMeasureSpec(mBitmap.getHeight(), MeasureSpec.EXACTLY);
			point.y = 0;
		}

		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		final int centerX = point.x + mBitmap.getWidth() / 2;
		final int centerY = point.y + mBitmap.getHeight() / 2;

		//绘制上半部
		canvas.save();
		canvas.clipRect(0, 0, getWidth(), centerY);
		canvas.drawBitmap(mBitmap, point.x, point.y, mPaint);
		canvas.restore();

		//绘制下半部
		canvas.save();
		if(degree <= 90){
			canvas.clipRect(0, centerY, getWidth(), getHeight());
		}else {
			canvas.clipRect(0,0,getWidth(),centerY);
		}

		mCamera.save();
		mCamera.rotateX(degree);
		canvas.translate(centerX, centerY);
		mCamera.applyToCanvas(canvas);
		canvas.translate(-centerX, -centerY);
		mCamera.restore();

		canvas.drawBitmap(mBitmap, point.x, point.y, mPaint);
		canvas.restore();
	}
}
